<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户类型" prop="pid">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择分类"
          clearable
        >
          <el-option
            v-for="dict in options"
            :key="dict.id"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户名称" prop="param">
        <el-input
          v-model="queryParams.param"
          placeholder="请用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="日期" prop="date">
        <el-date-picker
          v-model="queryParams.date"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      border
      v-loading="loading"
      :data="goodsList"
      @selection-change="handleSelectionChange"
      
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="永久跟随ID" align="center" prop="text" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="用户类型" align="center" prop="level">
      </el-table-column>
      <el-table-column label="总业绩" align="center" prop="total">

      </el-table-column>

      <el-table-column label="总业绩" align="center" prop="yearSale">
        <template slot="header">
          <div>年业绩</div>
          <div>{{ queryParams.date.slice(0, 4)  }}</div>
        </template>
      </el-table-column>
      <el-table-column label="月业绩" align="center" prop="monthSale">
        <template slot="header">
          <div>月业绩</div>
          <div>{{ queryParams.date.slice(0, 7) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="日业绩" align="center" prop="daySale" >
        <template slot="header">
          <div>日业绩</div>
          <div>{{ queryParams.date }}</div>
        </template>
      </el-table-column>
      <el-table-column label="总单量" align="center" prop="count">
      </el-table-column>

      <!-- <el-table-column label="零售价" align="center" prop="price" />
      <el-table-column label="详情" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['api:goods:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form
        ref="form"
        style="font-size: 42px"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="总业绩"> {{ info.total }} </el-form-item>
        <el-form-item label="当月业绩"> {{ info.monthly }} </el-form-item>
        <el-form-item label="修改日期" prop="createTime">
          <el-date-picker
            v-model="form.createTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="业绩类型" prop="title">
          <el-select v-model="value" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="金额" prop="price">
          <el-input-number v-model="form.price"> </el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :rows="2"
            v-model="form.remark"
            placeholder="请输入显示顺序"
        /></el-form-item>
        <el-form-item label="图片备注" prop="img">
          <image-upload v-model="form.img" />
        </el-form-item>

        <el-divider content-position="center">业绩修改记录</el-divider>

        <el-table :data="performanceList" border>
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="日期" prop="createTime" align="center">
          </el-table-column>

          <el-table-column label="金额" prop="price" align="center">
          </el-table-column>
          <el-table-column label="备注" prop="remark" align="center">
          </el-table-column>
          <el-table-column label="图片备注" prop="price" align="center">
            <template slot-scope="scope">
              <image-preview :src="scope.row.img" :width="50" :height="50" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGoods,
  delGoods,
  addGoods,
  updateGoods,
  listVip,
  listCate,
} from "@/api/api/goods";

import {
  dictList,
  delAgent,
  delinfo,
  achlist,
  getAch,
  performance,
} from "@/api/api/cate";
import { options } from "runjs";

export default {
  name: "Goods",
  dicts: ["user_vip"],
  data() {
    return {
      value: "",
      options: [
        {
          value: "选项1",
          label: "增加",
        },
        {
          value: "选项2",
          label: "减少",
        },
      ],

      options: [
        {
          value: "2",
          label: "代理",
        },
        {
          value: "1",
          label: "客服",
        },
      ],
      value: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDengji: [],
      checkedgg: [],
      cateList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      infoIds: [],
      // 商品管理表格数据
      goodsList: [],
      // 等级价格表格数据
      dengjiList: [],
      ggList: [],
      // 弹出层标题
      title: "",
      value1: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: "2",
        param: "",
        date: "",
      },
      // 表单参数
      form: {},
      yhList: [],
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "日期不能为空", trigger: "change" },
        ],
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        url: [{ required: true, message: "图片不能为空", trigger: "blur" }],
        price: [{ required: true, message: "金额不能为空", trigger: "blur" }],
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        store: [
          {
            required: true,
            message: "库存不能为空",
            trigger: ["blur", "change"],
          },
        ],
        warnStore: [
          {
            required: true,
            message: "库存不能为空",
            trigger: ["blur", "change"],
          },
        ],
      },
      info: {},
      performanceList: [],
      typeDict: [],
    };
  },
  created() {
    this.getList();
    this.getYhList();
    this.getFlList();
    dictList().then((res) => {
      this.typeDict = res.data;
    });
  },
  methods: {
    getFlList() {
      this.loading = true;
      // listCate(this.queryParams).then((response) => {
      //   this.cateList = response.rows;
      // });
    },
    changeYh(val) {
      val.userId = this.yhList.find((i) => i.userVip == val.title).id;
    },
    /** 查询商品管理列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.date) {
        const formattedDate = this.formatDateString(this.queryParams.date);
        this.queryParams.year = formattedDate.year;
        this.queryParams.month = formattedDate.month;
        this.queryParams.day = formattedDate.day;
      } else {
        this.queryParams.year = "";
        this.queryParams.month = "";
        this.queryParams.day = "";
      }

      achlist(this.queryParams).then((response) => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取用户等级
    getYhList() {
      listVip().then((res) => {
        console.log(res, "asdasdasda");
        this.yhList = res.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        createTime: "",
        price: "",
        remark: "",
        userId: "",
        img: "",
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      console.log(this.ids, "this.ids");
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.userId = row.id;
      const id = row.id || this.ids;
      getAch(id).then((response) => {
        this.info = response.data;
        this.performanceList = response.data.performanceList;
        this.open = true;
        this.title = "业绩管理";
      });
    },

    formatDateString(dateString) {
      const date = new Date(dateString);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");

      return { year, month, day };
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          performance(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
<style>
.alarm-row {
  background-color: #f56c6c !important;
}
</style>
